package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.model.Contact;
import com.crm.mapper.ContactMapper;
import com.crm.model.vo.ContactVO;
import com.crm.service.IContactService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公司联系人 服务实现类
 * </p>
 *
 * @author zjd
 * @since 2021-04-01
 */
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements IContactService {

    @Override
    public void deleteCompanyContasct(Long companyId) {
        QueryWrapper<Contact> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Contact::getCompanyId, companyId);
        List<Contact> list = list(wrapper);
        if (list !=null && list.size()>0){
            for (Contact contact : list) {
                contact.setCompanyId(0L);
                baseMapper.updateById(contact);
            }
        }
    }

    @Override
    public List<ContactVO> findByindexIdAndType(Long companyId) {
        QueryWrapper<Contact> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Contact::getCompanyId, companyId);
        List<Contact> list = list(wrapper);
        ArrayList<ContactVO> vos = new ArrayList<>();
        if (list !=null && list.size()>0){
            for (Contact contact : list) {
                ContactVO contactVO = BeanUtil.copyProperties(contact, ContactVO.class);
                vos.add(contactVO);
            }
        }
        return vos;
    }
}
